﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagEditorViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="TagEditorViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Tag.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common.BaseClasses;
    using Hisaab.Client.Common.Services;
    using Hisaab.Client.Model;
    using Hisaab.Common;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="TagEditorViewModel"/> type.
    /// </summary>
    public class TagEditorViewModel : WindowViewModel, INavigationAware
    {
        #region Data Members

        /// <summary>
        /// Tag manager
        /// </summary>
        private readonly ITagManager _tagManager;

        /// <summary>
        /// The session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Selected tag
        /// </summary>
        private Tag _selectedTag;

        /// <summary>
        /// Member variable _saveCommand
        /// </summary>
        private DelegateCommand _saveCommand;

        #endregion Data Members

        #region Constructors and initializers

        /// <summary>
        /// Initializes a new instance of the <see cref="TagEditorViewModel"/> class.
        /// </summary>
        public TagEditorViewModel()
            : this(ServiceLocator.Current.GetInstance<ITagManager>(), ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TagEditorViewModel"/> class.
        /// </summary>
        /// <param name="tagManager">The tag manager.</param>
        /// <param name="session">The session</param>
        public TagEditorViewModel(ITagManager tagManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(tagManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            Title = "Tag Editor";
            Icon = new BitmapImage(new Uri("pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/16/tag.png"));

            _tagManager = tagManager;
            _session = session;
        }

        #endregion Constructors and initializers

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether this instance is new tag.
        /// </summary>
        /// <value>
        /// True if this instance is new tag; otherwise, false.
        /// </value>
        public bool IsNewTag { get; set; }

        /// <summary>
        /// Gets the dialog type string.
        /// </summary>
        /// <value>
        /// The dialog type string.
        /// </value>
        public string DialogTypeString
        {
            get
            {
                return IsNewTag ? "Add" : "Edit";
            }
        }

        /// <summary>
        /// Gets or sets the selected tag.
        /// </summary>
        /// <value>
        /// The selected tag.
        /// </value>
        public Tag SelectedTag
        {
            get
            {
                return _selectedTag;
            }

            set
            {
                if (_selectedTag != value)
                {
                    _selectedTag = value;
                    RaisePropertyChangedEvent("SelectedTag");
                }
            }
        }

        /// <summary>
        /// Gets the save command.
        /// </summary>
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save, CanSave);
                }

                return _saveCommand;
            }
        }

        #endregion Properties

        #region Member Methods

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            if (IsNewTag)
            {
                SelectedTag.UserId = _session.LoggedUser;
                _tagManager.Add(SelectedTag.Map<Tag, Business.Entities.Tag>());
            }
            else
            {
                _tagManager.Update(SelectedTag.Map<Tag, Business.Entities.Tag>());
            }
        }

        /// <summary>
        /// Determines whether this instance can save.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance can save; otherwise, <c>false</c>.
        /// </returns>
        public bool CanSave()
        {
            return SelectedTag.Dirty && string.IsNullOrWhiteSpace(SelectedTag.Error);
        }

        #endregion Member Methods

        #region Implementation of INavigationAware

        /// <summary>
        /// Called when [navigated to].
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters["ID"] != null)
            {
                IsNewTag = false;
                int id = int.Parse(navigationContext.Parameters["ID"]);
                SelectedTag = _tagManager.GetById(id).Map<Business.Entities.Tag, Tag>();
            }
            else
            {
                IsNewTag = true;
                SelectedTag = new Tag();
            }

            SelectedTag.Dirty = false;
        }

        /// <summary>
        /// Determines whether [is navigation target] [the specified navigation context].
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        /// <returns>
        ///   <c>true</c> if [is navigation target] [the specified navigation context]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        /// <summary>
        /// Called when [navigated from].
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        #endregion Implementation of INavigationAware
    }
}